﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace RCREEE.Web.Classes
{
    public class Helper
    {

        public static GridView ExportToExcel(DataTable dataTable, List<string> validColumns)
        {
            try
            {
                List<string> toBeRemoved = new List<string>();
                foreach (DataColumn column in dataTable.Columns)
                {
                    if (!validColumns.Contains(column.ColumnName))
                    {
                        toBeRemoved.Add(column.ColumnName);
                    }
                }
                foreach (string columnName in toBeRemoved)
                {
                    dataTable.Columns.Remove(columnName);
                }
                GridView gridView = new GridView();
                gridView.DataSource = dataTable;
                //gridView.DataSource = sd.Select(x => new
                //                                       {
                //                                           Za3tar = x,
                //                                           Bandar = "dfg Banager 123"
                //                                       });


                //foreach (DataControlField item in gvMaster.Columns)
                //{
                //    DataControlField newItem = item;
                //    var ssdd = newItem.GetType();
                //    if (newItem.GetType().ToString().Contains("BoundField"))
                //    {
                //        gridView.Columns.Add(newItem);
                //    }
                //    else if (newItem.GetType().ToString().Contains("TemplateField"))
                //    {
                //        TemplateField temp = (TemplateField)newItem;
                //        TemplateField tempField = new TemplateField
                //                                      {ItemTemplate = temp.ItemTemplate, HeaderText = temp.HeaderText};

                //        gridView.Columns.Add(tempField );
                //    }
                //    //gridView.Columns.Add(new BoundField
                //    //{
                //    //    DataField = item.DataField,
                //    //    HeaderText = item.HeaderText
                //    //});
                //}
                gridView.AutoGenerateColumns = true;
                gridView.DataBind();

                gridView.DataBind();
                return gridView;
            }
            catch (Exception exc)
            {
                throw exc;
            }
        }
    }
}